#include "music_search.h"
#include "ui_music_search.h"
#include <QHeaderView>
#include "music.h"
music_search::music_search(QWidget *parent, music * pMusic) :
    QWidget(parent),
    ui(new Ui::music_search)
{
    ui->setupUi(this);
//    ui->tableView->setStyleSheet("border:2px solid rgb(20,196,188);");
    //表格线条颜色
    ui->tableView->setStyleSheet("QTableView {""gridline-color: rgb(20,196,188);" "}"
                                 "QTableView::item:selected { background-color: rgb(20,196,188); color: white; }");
    //表头线条颜色
    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {""border: 1px solid rgb(20,196,188);""}"
                                                     "QHeaderView::section { background-color: rgb(255,255,255); }");

    song_name_ing = " ";
   //设置列表表格
   pModel = new QStandardItemModel(10,4,ui->tableView);
   QStringList list = QStringList()<<"歌名"<<"歌手"<<"专辑"<<"歌曲时长";

   pMusic1 = pMusic;

//   QHeaderView * header = ui->tableView->horizontalHeader();
//   header->setFixedSize(60,250);
   pModel->setHorizontalHeaderLabels(list);
//   pModel->setItem()
   //访问网址
  manage = new QNetworkAccessManager(this);
  connect(manage,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyfinished(QNetworkReply *)));

  //播放音乐
 playlist =  new QMediaPlaylist();
 player =  new QMediaPlayer();
 player->setPlaylist(playlist);
// player->setVolume(100);设置音量

 //获取播放位置
 connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(positionChangedslot(qint64)));
 connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(durationChangedslot(qint64)));

}

music_search::~music_search()
{
    delete ui;
}
//读取网站信息的槽函数
void music_search::replyfinished(QNetworkReply *reply)
{
    //QNetworkRequest::HttpStatusCodeAttribute 是网址访问返回的状态码
        qDebug()<<"--------\"hello\"--------"<<reply->attribute(
                      QNetworkRequest::HttpStatusCodeAttribute)<<endl;
        if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200)
        {
            qDebug()<<"访问服务器成功"<<endl;
    //               qDebug()<<QString(reply->readAll())<<endl;
            parseJon(reply->readAll());
        }
}

void music_search::positionChangedslot(qint64 position)
{
    qDebug()<<position<<endl;
    pMusic1->Slidering(position);


}

void music_search::durationChangedslot(qint64 duration)
{
    qDebug()<<duration<<"毫秒"<<endl;
    pMusic1->Sliderall(duration);
}


//获取json数据的内容
//思路  如果是array->读取array[]->转成toObject->在读取.value("对象属性")->在转成tostring()就变成QString格式可以正常读取了
void music_search::parseJon(QByteArray byteArray)
{
    QString song_name;//歌名
    QString singger_name;//歌手
    QString song_album_name;//专辑
    song_name_list.clear();
    float minutes;
    int id;
    playlist->clear();

    //格式转换
    QJsonDocument doc = QJsonDocument::fromJson(byteArray);
    if(doc.isObject())
    {
        QJsonObject obj1 = doc.object();
        if(obj1.contains("result")==false)
        {
            qDebug()<<"没有result对象，错误！"<<endl;
            return;
        }
        qDebug()<<444<<endl;
        //obj1对象中确定百分百有这个值才能这样，不然需要obj1.contains("result")判断为true则存在
        QJsonValue val =obj1.value("result");
        QJsonObject songobj  = val.toObject();
        if(songobj.contains("songs") == false)
        {
            qDebug()<<"没有songs对象，错误！"<<endl;
            return;
        }
        val =  songobj.value("songs");
        qDebug()<<333<<endl;
        if(val.isArray())
        {
            QJsonArray songsarr   =  val.toArray();
            qDebug()<<"一共搜索到"<<songsarr.size()<<"歌曲"<<endl;
            //遍历歌曲信息
            for (int i = 0;i <songsarr.size();i++) {



                if(songsarr[i].isObject() == false)
                {
                    qDebug()<<"songsarr不是一个对象，错误！"<<endl;
                    return;
                }
                QJsonObject song_objdata =  songsarr[i].toObject();
                //获取歌名
                if(song_objdata.contains("name"))
                {
                    val = song_objdata.value("name");
                    song_name = val.toString();
                }
                //获取歌手
                if(song_objdata.contains("artists"))
                {
                    if(song_objdata.value("artists").isArray())
                    {
                        //                        qDebug()<<"artists是一个数组"<<endl;
                        QJsonArray singger_arr = song_objdata.value("artists").toArray();
                        for (int j = 0;j<singger_arr.size();j++) {
                            singger_name.append(singger_arr[j].toObject().value("name").toString());
                            singger_name.append(" ");
                        }
                    }
                }
                //获取专辑
                if(song_objdata.contains("album"))
                {
                    if(song_objdata.value("album").isObject())
                    {
                        QJsonObject song_album_obj = song_objdata.value("album").toObject();
                        song_album_name = song_album_obj.value("name").toString();
                    }
                }
                //获取时长
                if(song_objdata.contains("duration"))
                {
                   if(song_objdata.value("duration").isDouble())
                    {
                        minutes = (song_objdata.value("duration").toDouble()/(1000*60.0f));
                    }
                }
                //获取id
                if(song_objdata.contains("id"))
                {
                    if(song_objdata.value("id").isDouble())
                     {
                         id = song_objdata.value("id").toDouble();

                     }
                }

                playlist->addMedia(QUrl(QString("http://music.163.com/song/media/outer/url?id=%0.mp3").arg(id)));
                song_name_list.append(song_name);
                QStandardItem *item1 = new QStandardItem(song_name);
                QStandardItem *item2 = new QStandardItem(singger_name);
                QStandardItem *item3 = new QStandardItem(song_album_name);
                QStandardItem *item4 = new QStandardItem(QString("%0 m").arg(minutes));
                singger_name.clear();
                pModel->setItem(i, 0, item1);
                pModel->setItem(i, 1, item2);
                pModel->setItem(i, 2, item3);
                pModel->setItem(i, 3, item4);

            }

        }
    }
    qDebug()<<111<<endl;
    ui->tableView->setModel(pModel);
    ui->tableView->setColumnWidth(0,250);
    ui->tableView->setColumnWidth(1,250);
    ui->tableView->setColumnWidth(2,250);
    ui->tableView->setColumnWidth(3,250);


    ui->tableView->setRowHeight(0,60);
    ui->tableView->setRowHeight(1,60);
    ui->tableView->setRowHeight(2,60);
    ui->tableView->setRowHeight(3,60);
    ui->tableView->setRowHeight(4,60);
    ui->tableView->setRowHeight(5,60);
    ui->tableView->setRowHeight(6,60);
    ui->tableView->setRowHeight(7,60);
    ui->tableView->setRowHeight(8,60);
    ui->tableView->setRowHeight(9,60);

}
//切换歌曲的槽函数
void music_search::on_tableView_clicked(const QModelIndex &index)
{
    qDebug()<<index.row()<<endl;
    song_list_num = index.row();
    playlist->setCurrentIndex(index.row());
    song_name_ing = song_name_list.at(index.row());
    pMusic1->songename(song_name_ing);
    player->setVolume(pMusic1->voice_mun);
    player->play();
//    player->state();
}

//



